<?

	/**************************
		Forms
		Martin Loeffen
	**************************/
	
	$allForms = array();
	
	function getAllForms()
	{
		global $allForms;
		return $allForms;
	}
	
	// Form Object
	class Form {

		public $id, $children;
		
		public function __construct($id) 
		{
			global $allForms;
			
			$this->id 			= $id;
			$this->children 	= array(); 
			
			$allForms[] = $this;
		}
		
		public function add($formElement)
		{
			if (get_class($formElement) === 'FormElement')
			{
				$this->children[] = $formElement;
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function start()
		{
			global $context;
		
			echo '<form autocomplete="off" method="post" enctype="multipart/form-data" class="generiek-form" action="' . $context['action'] . '">';
			echo '<input type="hidden" name="formid" value="'.$this->id.'" method="post" />';
		}
		
		public function end()
		{
			echo '</form>';
		}
		
		public function children() 
		{
			return $this->children;
		}
		
		public function show()
		{
			$this->start();
				foreach ($this->children as $kid) $kid->show();
				
			echo '<button type="submit" name="Submit" class="submit btn blue">Verzenden</button>';
			
			$this->end();
  
		}
    
	}
	
	// Form Element Object
	class FormElement {

		public $name, $type, $value, $pattern, $warningMessage;
		
		public function __construct($name,$type='text',$value=null,$pattern = '.*',$warningMessage = 'Controleer dit veld', $values = array()) 
		{
			$this->type 				= $type;
			$this->name 				= $name;
			$this->value				= $value;
			$this->values 				= $values;
			$this->pattern 				= $pattern; 
			$this->warningMessage 		= $warningMessage;
		}
		
		public function show()
		{
			
			if ($this->type != 'checkbox' && $this->type != 'radio' && $this->type != 'hidden')
			{
				echo '<label>';
				echo '<span>' . $this->name . '</span>';
			}
			
			switch ($this->type)
			{
				case 'longtext':	
					echo '<textarea name="'.$this->name.'" data-pattern="'.$this->pattern.'" data-warning="'.$this->warningMessage.'">'.$this->value .'</textarea>'; 
					break;
				
				
				case 'password':	
					echo '<input type="password" name="'.$this->name.'" data-pattern="'.$this->pattern.'" data-warning="'.$this->warningMessage.'"  />'; 
					break;
				
				//value="'.$this->value .'"
				
				case 'dropdown':	
					echo '<select name="'.$this->name.'" data-warning="'.$this->warningMessage.'">'; 
					foreach ($this->values as $key => $value) 
						echo '<option value="'.$key.'" '. ($value==$this->value?'selected':'') . '>' . $value . '</option>'; 
					echo '</select>';
					break;

				case 'radio':	
					echo '<label>';
					echo '<span>' . $this->name . '</span>';
					foreach ($this->values as $key => $value)
						echo '<label><input type="radio" value="' . $key . '" name="' . $this->name . '" ' . (($value===$this->value)?'checked':'') . '/> ' . $value . '</label>';
					break;
				
				case 'checkbox':
					echo '<label>';
					echo '<span>' . $this->name . '</span>';
					echo '<label><input type="checkbox" name="' . $this->name . '" value="true" ' . (empty($this->value)?'':'checked') . '/> ' . $this->name . '</label>';
					break;	
					
				case 'multiple':	
					echo '<select multiple="multiple" name="'.$this->name.'[]" data-warning="'.$this->warningMessage.'">'; 
					foreach ($this->values as $key => $value) 
						echo '<option value="'.$key.'" '. ($value==$this->value?'selected':'') . '>' . $value . '</option>'; 
					echo '</select>';
					break;
				
				
				case 'date':
					echo '<input type="text" value="'.$this->value.'" class="datepicker" name="'.$this->name.'"  data-warning="'.$this->warningMessage.'"  />';
					break;
					
				case 'file':
					echo '<input size="40" type="file" value="'.$this->value.'" name="'.$this->name.'"  data-warning="'.$this->warningMessage.'"  />';
					break;
				
				case 'address':
					echo '<input type="text" value="'.$this->value.'" class="address" name="'.$this->name.'"  />';
					break;
				
				case 'number':
					echo '<input type="number" min="0" step="1" value="'.$this->value.'" class="number" name="'.$this->name.'"  />';
					break;
					
				case 'hidden':
					echo '<input type="hidden" value="'.$this->value.'" name="'.$this->name.'"  />';
					break;
					
				case 'read-only':
				case 'readonly':
				case 'disabled':
					echo '<input type="text" readonly="readonly" value="'.$this->value.'"  name="'.$this->name.'"  />';
					break;
				
				case 'text':	
				default:
					echo '<input type="text" value="'.$this->value.'" name="'.$this->name.'" pattern="' . $this->pattern . '" data-warning="'.$this->warningMessage.'" />';
			}
			
			echo '</label>';
		}
		
	}
	
	
	define("FORMS_LOADED",true);
?>